package com.nanhai.buddhist.college.module.collegebase.controller.admin.studentchange;

import com.nanhai.buddhist.college.module.collegebase.controller.admin.studentchange.vo.StudentChangeCreateReqVO;
import com.nanhai.buddhist.college.module.collegebase.controller.admin.studentchange.vo.StudentChangeExcelVO;
import com.nanhai.buddhist.college.module.collegebase.controller.admin.studentchange.vo.StudentChangeExportReqVO;
import com.nanhai.buddhist.college.module.collegebase.controller.admin.studentchange.vo.StudentChangePageReqVO;
import com.nanhai.buddhist.college.module.collegebase.controller.admin.studentchange.vo.StudentChangeRespVO;
import com.nanhai.buddhist.college.module.collegebase.controller.admin.studentchange.vo.StudentChangeUpdateReqVO;
import com.nanhai.buddhist.college.module.collegebase.convert.studentchange.StudentChangeConvert;
import com.nanhai.buddhist.college.module.collegebase.dal.dataobject.studentchange.StudentChangeDO;
import com.nanhai.buddhist.college.module.collegebase.service.studentchange.StudentChangeService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.nanhai.buddhist.college.framework.common.pojo.PageResult;
import com.nanhai.buddhist.college.framework.common.pojo.CommonResult;
import static com.nanhai.buddhist.college.framework.common.pojo.CommonResult.success;

import com.nanhai.buddhist.college.framework.excel.core.util.ExcelUtils;

import com.nanhai.buddhist.college.framework.operatelog.core.annotations.OperateLog;
import static com.nanhai.buddhist.college.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.nanhai.buddhist.college.module.collegebase.controller.admin.studentchange.vo.*;

@Api(tags = "管理后台 - 学籍信息异动")
@RestController
@RequestMapping("/collegebase/student-change")
@Validated
public class StudentChangeController {

    @Resource
    private StudentChangeService studentChangeService;

    @PostMapping("/create")
    @ApiOperation("创建学籍信息异动")
    @PreAuthorize("@ss.hasPermission('collegebase:student-change:create')")
    public CommonResult<String> createStudentChange(@Valid @RequestBody StudentChangeCreateReqVO createReqVO) {
        return success(studentChangeService.createStudentChange(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新学籍信息异动")
    @PreAuthorize("@ss.hasPermission('collegebase:student-change:update')")
    public CommonResult<Boolean> updateStudentChange(@Valid @RequestBody StudentChangeUpdateReqVO updateReqVO) {
        studentChangeService.updateStudentChange(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除学籍信息异动")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('collegebase:student-change:delete')")
    public CommonResult<Boolean> deleteStudentChange(@RequestParam("id") String id) {
        studentChangeService.deleteStudentChange(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得学籍信息异动")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('collegebase:student-change:query')")
    public CommonResult<StudentChangeRespVO> getStudentChange(@RequestParam("id") String id) {
        StudentChangeDO studentChange = studentChangeService.getStudentChange(id);
        return success(StudentChangeConvert.INSTANCE.convert(studentChange));
    }

    @GetMapping("/list")
    @ApiOperation("获得学籍信息异动列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('collegebase:student-change:query')")
    public CommonResult<List<StudentChangeRespVO>> getStudentChangeList(@RequestParam("ids") Collection<String> ids) {
        List<StudentChangeDO> list = studentChangeService.getStudentChangeList(ids);
        return success(StudentChangeConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得学籍信息异动分页")
    @PreAuthorize("@ss.hasPermission('collegebase:student-change:query')")
    public CommonResult<PageResult<StudentChangeRespVO>> getStudentChangePage(@Valid StudentChangePageReqVO pageVO) {
        PageResult<StudentChangeDO> pageResult = studentChangeService.getStudentChangePage(pageVO);
        PageResult<StudentChangeRespVO> pageResultVo = StudentChangeConvert.INSTANCE.convertPage(pageResult);
        studentChangeService.setOldData(pageResultVo);
        return success(pageResultVo);
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出学籍信息异动 Excel")
    @PreAuthorize("@ss.hasPermission('collegebase:student-change:export')")
    @OperateLog(type = EXPORT)
    public void exportStudentChangeExcel(@Valid StudentChangeExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<StudentChangeDO> list = studentChangeService.getStudentChangeList(exportReqVO);
        // 导出 Excel
        List<StudentChangeExcelVO> datas = StudentChangeConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "学籍信息异动.xls", "数据", StudentChangeExcelVO.class, datas);
    }

    @PutMapping("/check")
    @ApiOperation("审核学籍信息异动")
    @PreAuthorize("@ss.hasPermission('collegebase:student-change:update')")
    public CommonResult<Boolean> checkStudentChange(@Valid @RequestBody StudentChangeCheckReqVO req) {
        studentChangeService.checkStudentChange(req);
        return success(true);
    }

}
